package com.xjzd.std.variance;

import java.util.ArrayList;
import java.util.List;

public class Contravariance {

    private static class Animal {
        public void sound() {
            System.out.println("Animal sound");
        }
    }

    private static class Dog extends Animal {
        @Override
        public void sound() {
            System.out.println("Bark");
        }
    }

    private static void addDog(List<? super Dog> animals) {
        animals.add(new Dog()); // 可以添加 Dog 类型的对象
    }

    public static void main(String[] args) {
        List<Animal> animals = new ArrayList<>();
        addDog(animals);

        animals.forEach(Animal::sound);
    }
}
